/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * This class represents a user from the user table.
 *
 * @author Anton Strack
 */
public class User {

    /**
     * The unique id identifying the user.
     */
    private int id;
    /**
     * A unique name used to represent the user on the application.
     */
    private String name;
    /**
     * The email address for the user.
     */
    private String email;
    /**
     * The password belonging the user and used for authentication.
     */
    private String password;
    /**
     * The role of the user indicating their access rights on the application.
     */
    private int roleId;
    /**
     * The current login date of the user.
     */
    private Timestamp loginDate;
    /**
     * The previous login date of the user.
     */
    private Timestamp lastLoginDate;
    /**
     * The user specific salt value used to strengthen the password hash.
     */
    private String salt;
    /**
     * Special runtime attributes for the user.
     */
    protected Map<String, String> derivedAttributes;

    public User() {
        this.derivedAttributes = new HashMap<String, String>();
    }

    /**
     * Get the derived string value matching the key. Derived values are values
     * not stored in the bean's database table. They are either the matching
     * names of id values stored in the bean table or they are other related
     * values.
     *
     * @param key Column name key for retrieving the derived attribute. As long
     * as setDerivedValue() was used to set the derived value, case does not
     * matter for the key.
     * @return the derived attribute value matching the key or an empty string
     * if the key doesn't exist.
     */
    public String getDerivedValue(String key) {
        key = key.toLowerCase();
        if (this.derivedAttributes.containsKey(key)) {
            return this.derivedAttributes.get(key);
        }
        return "";
    }

    /**
     * Get the derived int value matching the key. Derived values are values not
     * stored in the bean's database table. They are either the matching names
     * of id values stored in the bean table or they are other related values.
     * This method attempts to return the derived value as an integer.
     *
     * @param key Column name key for retrieving the derived attribute. As long
     * as setDerivedValue() was used to set the derived value, case does not
     * matter for the key.
     * @return the derived attribute value matching the key or 0 if the key
     * doesn't exist
     */
    public int getDerivedInt(String key) {
        String value;
        key = key.toLowerCase();
        if (this.derivedAttributes.containsKey(key)) {
            value = this.derivedAttributes.get(key);
            if (value != null && value.isEmpty() == false) {
                Integer i = Integer.parseInt(value);
                return i.intValue();
            }
        }
        // return zero if any above condition fails
        return 0;
    }

    /**
     * Set the name of the related id value.
     *
     * @param nameFor the key name used to get the value from the bean.
     * Generally follows the pattern StateName if the bean has a field StateId.
     * @param name the name that is related to the id value
     */
    public void setDerivedValue(String nameFor, String name) {
        // convert the name to lowercase to reduce bugs caused by incorrect case.
        this.derivedAttributes.put(nameFor.toLowerCase(), name);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the roleId
     */
    public int getRoleId() {
        return roleId;
    }

    /**
     * @param roleId the roleId to set
     */
    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    /**
     * @return the loginDate
     */
    public Timestamp getLoginDate() {
        return loginDate;
    }

    /**
     * @param loginDate the loginDate to set
     */
    public void setLoginDate(Timestamp loginDate) {
        this.loginDate = loginDate;
    }

    /**
     * @return the lastLoginDate
     */
    public Timestamp getLastLoginDate() {
        return lastLoginDate;
    }

    /**
     * @param lastLoginDate the lastLoginDate to set
     */
    public void setLastLoginDate(Timestamp lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

    /**
     * @return the salt
     */
    public String getSalt() {
        return salt;
    }

    /**
     * @param salt the salt to set
     */
    public void setSalt(String salt) {
        this.salt = salt;
    }
}
